# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  if Vagrant.has_plugin?("vagrant-vbguest") then
    config.vbguest.auto_update = false
  end

  config.ssh.insert_key = false

  # 这个设置针对无法被 vagrant 识别的国产系统，例如 openEuler。强制指定按哪个通用系统来配置。
  # config.vm.guest = "centos"

  # How many vm do you want?
  N = 13

  config.vm.provider "virtualbox" do |vb|
    vb.memory = 1024 * 4
    vb.cpus = 2
    vb.customize ["modifyvm", :id, "--uartmode1", "disconnected"]
    vb.linked_clone = true
    vb.check_guest_additions = false
  end

  config.vm.provision "shell" do |s|
    s.inline = <<-SHELL
    sed -i "s|PasswordAuthentication no|PasswordAuthentication yes|g" /etc/ssh/sshd_config
    echo "nameserver 223.5.5.5" | tee /etc/resolv.conf
    echo "NETWORKING_IPV6=no" | tee -a /etc/sysconfig/network
    echo "IPV6INIT=no" | tee -a /etc/sysconfig/network-scripts/ifcfg-eth1
    echo "IPV6_AUTOCONF=no" | tee -a /etc/sysconfig/network-scripts/ifcfg-eth1
    echo "IPV6_DEFROUTE=no" | tee -a /etc/sysconfig/network-scripts/ifcfg-eth1
    echo "IPV6_PEERDNS=no" | tee -a /etc/sysconfig/network-scripts/ifcfg-eth1
    echo "IPV6_PEERROUTES=no" | tee -a /etc/sysconfig/network-scripts/ifcfg-eth1
    echo "IPV6FORWARDING=no" | tee -a /etc/sysconfig/network-scripts/ifcfg-eth1
    echo "IPV6_AUTOTUNNEL=no" | tee -a /etc/sysconfig/network-scripts/ifcfg-eth1

    timedatectl set-timezone "Asia/Shanghai"
    timedatectl

    dnf install -y epel-release
    /usr/bin/crb enable

    dnf install -y bzip2 expect net-tools chrony tar gzip readline-devel \
      patch ncurses-devel libaio-devel glibc-devel flex bison nano htop \
      python3 NetworkManager-initscripts-updown byobu python3-pexpect python3-cryptography

    ifdown eth1
    ifup eth1
    /bin/systemctl restart sshd.service
    SHELL
  end

  #Disabling the default /vagrant share
  config.vm.synced_folder ".", "/vagrant", disabled: true

  (1..N).each do |i|
    config.vm.define "elk#{i}" do |node|
      # node.vm.box = "generic/rhel7"
      # node.vm.box = "almalinux/8"
      node.vm.box = "bento/rockylinux-8.10"
      node.vm.network "private_network", ip: "192.168.56.#{10+i}"
    end
  end
end
